package xhhx.akka;

import akka.actor.AbstractActor;
import akka.actor.Props;
import com.google.protobuf.MessageLite;
import io.netty.channel.Channel;
import xhhx.cache.PlayerCache;
import xhhx.manager.MsgManager;
import xhhx.network.S2cMsg;

public class ConnectActor extends AbstractActor {
    private Channel channel;

    private ConnectActor(Channel ch) {
        this.channel = ch;
    }

    static public Props props(Channel ch) {
        return Props.create(ConnectActor.class, () -> new ConnectActor(ch));
    }

    @Override
    public void preStart() throws Exception {
        super.preStart();
    }

    @Override
    public void postStop() throws Exception {
        super.postStop();
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(MsgResponder.class, c2SMsg -> {
                    receiveC2S(c2SMsg);
                })
                .build();
    }

    private void receiveC2S(MsgResponder msg) {
        S2cMsg s2c = new S2cMsg(msg.msgID, (MsgManager.Processor(msg.msgID, msg.receive, msg.playerCache)));
        channel.writeAndFlush(s2c);
    }

    static public class MsgResponder {
        public final int msgID;
        public final MessageLite receive;
        public final PlayerCache playerCache;

        public MsgResponder(int msgId, MessageLite receive, PlayerCache playerCache) {
            this.msgID = msgId;
            this.receive = receive;
            this.playerCache = playerCache;

        }
    }
}
